package level

import (
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
)

type LevelDao struct {
	db *db.MongoMiddleware
}

func NewLevelDao() *LevelDao {
	return &LevelDao{
		db: db.Db,
	}
}
func (l *LevelDao) userLevelInfo(userId string) (UserLevelInfo, error) {
	var info UserLevelInfo
	err := l.db.FindOne(TBL_LEVEL, bson.M{"user_id": userId}, &info)
	if err != nil {
		return UserLevelInfo{}, err
	}
	return info, nil
}
func (l *LevelDao) insert(data UserLevelInfo) error {
	return l.db.Insert(TBL_LEVEL, data)
}
func (l *LevelDao) Insert(data UserLevelInfo) error {
	return l.db.Insert(TBL_LEVEL, data)
}
func (l *LevelDao) updateUserLevelProgress(userId string, progress int) error {
	filter := bson.M{"user_id": userId}
	update := bson.M{"progress": progress}
	return l.db.Update(TBL_LEVEL,
		filter,
		bson.M{"$set": update})
}
func (l *LevelDao) updateCurrentLevelProgressLevelIds(userId string, currentLevel, progress int, levelIds []int) error {
	filter := bson.M{"user_id": userId}
	update := bson.M{
		"current_level": currentLevel,
		"progress":      progress,
		"level_ids":     levelIds}
	return l.db.Update(TBL_LEVEL, filter, bson.M{"$set": update})
}
func (l *LevelDao) updatePeiXunIds(userId string, zhaoGongList map[int64]ZhaoGongCard) error {
	filter := bson.M{"user_id": userId}
	update := bson.M{"pei_xun_card_ids": zhaoGongList}
	return l.db.Update(TBL_LEVEL, filter, bson.M{"$set": update})
}
func (l *LevelDao) updateJiLiTime(userId string, JiLiTime int64) error {
	filter := bson.M{"user_id": userId}
	update := bson.M{"ji_li_time": JiLiTime}
	return l.db.Update(TBL_LEVEL, filter, bson.M{"$set": update})
}
func (l *LevelDao) GetPlayerLevelInfo(userID string) (*UserLevelInfo, error) {
	var info UserLevelInfo
	err := l.db.FindOne(TBL_LEVEL, bson.M{"user_id": userID}, &info)
	return &info, err
}
